package com.jingtai.shop.controller.goods;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.common.webfacade.BasePageBO;
import com.jingtai.common.webfacade.IdBO;
import com.jingtai.core.entity.shop.MerchantsGoods;
import com.jingtai.core.template.BaseController;
import com.jingtai.shop.common.template.ShopBaseController;
import com.jingtai.shop.service.goods.*;
import com.jingtai.shop.webfacade.bo.goods.GoodsDetailsBO;
import com.jingtai.shop.webfacade.bo.goods.LevelGoodsBO;
import com.jingtai.shop.webfacade.bo.goods.SearchGoodsBO;
import com.jingtai.shop.webfacade.bo.goods.StoreGoodsBo;
import com.jingtai.shop.webfacade.vo.category.GoodsCategoryTwoVO;
import com.jingtai.shop.webfacade.vo.category.GoodsCategoryVO;
import com.jingtai.shop.webfacade.vo.goodsdetail.GoodsDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "商城-轮播图")
@RestController
@RequestMapping("/shop/goods")
@CrossOrigin(origins = "*")
public class GoodsController extends ShopBaseController {

    @Resource
    private GoodsCategoryService categoryService;
    @Resource
    private MerchantsGoodsService goodsService;
    @Resource
    private MerchantsGoodsDetailService goodsDetailService;
    @Resource
    private MerchantsGoodsWarehouseService goodsWarehouseService;
    @Resource
    private GoodsSpecificationsCustomService goodsSpecificationsCustomService;
    /**
     * 首页-查询一级商品分类
     */
    @ApiOperation(value = "查询一级商品分类")
    @PostMapping("/listCategoryByLevelOne")
    public Map<String, Object> listCategoryByLevelOne() {
        List<GoodsCategoryVO> categoryVOList = categoryService.listCategoryByLevelOne();
        return returnResultMap(ResultMapInfo.GETSUCCESS, categoryVOList);
    }


    /**
     * 点击一级商品,查询二级和三级商品
     */
    @ApiOperation(value = "查询二级级商品分类")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/listGoodsCategoryLevelTwo")
    public Map<String, Object> listGoodsCategoryLevelTwo(@RequestBody IdBO bo) {
        bo.validate();
        List<GoodsCategoryTwoVO> categoryTwoVOList = categoryService.listGoodsCategoryLevelTwo(bo.getId());
        return returnResultMap(ResultMapInfo.GETSUCCESS, categoryTwoVOList);
    }


    /**
     * 三级分类id 查询商品列表
     */
    @ApiOperation(value = "查询商品列表根据三级分类id")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/pageGoodsByLevel")
    public Map<String, Object> pageGoodsByLevel(@RequestBody LevelGoodsBO bo) {
        bo.validate();
        Page<MerchantsGoods> merchantsGoodsPage = goodsService.pageGoodsByLevel(bo.getThreeId(), bo.getPageNo(), bo.getPageSize());
        return returnResultMap(ResultMapInfo.GETSUCCESS, merchantsGoodsPage);
    }

    /**
     * 首页 推荐商品(优选)
     */
    @ApiOperation(value = "首页-查询推荐商品")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/pageGoodsByRecommend")
    public Map<String, Object> pageGoodsByRecommend(@RequestBody BasePageBO bo) {
        bo.validate();
        Page<MerchantsGoods> merchantsGoodsPage = goodsService.pageGoodsByRecommend(bo.getPageNo(), bo.getPageSize());
        return returnResultMap(ResultMapInfo.GETSUCCESS, merchantsGoodsPage);
    }

    /**
     * 首页-关键字搜索商品
     */
    @ApiOperation(value = "首页-关键字搜索商品")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/searchMerchantsGoods")
    public Map<String, Object> searchMerchantsGoods(@RequestBody SearchGoodsBO bo) {
        bo.validate();
        Page<MerchantsGoods> merchantsGoodsPage = goodsService.searchMerchantsGoods(bo.getKeyWord(), bo.getPageNo(), bo.getPageSize());
        return returnResultMap(ResultMapInfo.GETSUCCESS, merchantsGoodsPage);
    }


    /**
     * 根据商品编码查询商品详情
     */
    @ApiOperation(value = "根据商品编码查询商品详情")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/queryGoodsDetail")
    public Map<String, Object> queryGoodsDetail(@RequestBody GoodsDetailsBO bo) {
        bo.validate();
        Long userId = 0L;
        try {
            userId = getUserId();
        }catch (Exception e){

        }
        GoodsDetailVO goodsDetailVO = goodsDetailService.queryGoodsDetail(bo.getGoodsCode(),userId);
        return returnResultMap(ResultMapInfo.GETSUCCESS, goodsDetailVO);
    }

    /**
     * 根据商品编码查询商品详情
     */
    @ApiOperation(value = "根据商品编码查询商品自定义规格")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/queryGoodsCustom")
    public Map<String, Object> queryGoodsCustom(@RequestBody GoodsDetailsBO bo) {
        bo.validate();
        List<JSONObject> jsonObject = goodsSpecificationsCustomService.queryGoodsCustom(bo.getGoodsCode());
        return returnResultMap(ResultMapInfo.GETSUCCESS, jsonObject);
    }

    /**
     * 商品详请->展示商品的规格参数
     */
    @ApiOperation(value = "展示商品的规格参数")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/showShopAttribute")
    public Map<String, Object> showShopAttribute(@RequestBody GoodsDetailsBO bo) {
        bo.validate();
        Map<String, Object> shopAttribute = goodsWarehouseService.showShopAttribute(bo.getGoodsCode());
        return returnResultMap(ResultMapInfo.GETSUCCESS, shopAttribute);
    }

    /**
     * 根据店铺Id 分页查询商品商品
     */
    @ApiOperation(value = "根据店铺Id 分页查询商品商品")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/showStoreGoods")
    public Map<String, Object> showStoreGoods(@RequestBody StoreGoodsBo bo) {
        bo.validate();
        Page<MerchantsGoods> merchantsGoodsPage = goodsWarehouseService.showStoreGoods(bo.getStoreId(),bo.getType(), bo.getPageNo(), bo.getPageSize());
        return returnResultMap(ResultMapInfo.GETSUCCESS, merchantsGoodsPage);
    }
}
